import json
from crawler.extractors import BaseExtractor
from crawler.fields import ContainerField, TextField, AttrField

__all__ = ['PorterMainExtractor', 'PorterImageMainInfoExtractor']


class PorterMainInfoExtractor(BaseExtractor):
    container = ContainerField(xpath_selector='//div[@class="ProductGrid52 ProductListWithLoadMore52__listingGrid"]/a')
    spu_id = AttrField(xpath_selector='./div', attr='id')
    brand = TextField(xpath_selector='./div//span[@dir="ltr"]')
    title = TextField(xpath_selector='./div//span[@class="ProductItem25__name"]')
    cost_price = TextField(
        xpath_selector='./div//span[@itemprop="price"]')
    wages = TextField(
        xpath_selector='./div//span[@itemprop="price"]')
    url = AttrField(xpath_selector='.', attr='href')
    # size = TextField(xpath_selector='.//a/div[@class="colorSize"]//span', many=True)


class PorterImageMainInfoExtractor(BaseExtractor):
    container = ContainerField(
        xpath_selector='//div[@class="ImageCarousel87__thumbnails ProductDetailsPage87__imageCarouselThumbnails"]/div')
    image_url = AttrField(xpath_selector='.//img[@class="Image18__image Image18__image--noScript"]', attr='src')


class PorterSizeMainInfoExtractor(BaseExtractor):
    # 这种包含状态，比如售罄
    size = AttrField(xpath_selector='//ul[@class="GridSelect11"]/li/label', many=True, attr='aria-label')
    size_other = TextField(xpath_selector='//span[@class="SizeSelect86__oneSizeLabel"]')


class PorterMainExtractor:
    @classmethod
    def get_item(cls, response):
        return cls._extract(response)

    @classmethod
    def _extract(cls, response):
        task = response.meta['task']
        sex = task['sex']
        category = task['url']['category']
        # sex = '男'
        # types = 'clothing'
        info_list = list()
        for item in PorterMainInfoExtractor.get_items(response):
            info_dict = dict()
            info_dict['spu_id'] = item.spu_id.split('pid-')[1]
            # 品牌
            info_dict['brand'] = item.brand
            # 商标题
            info_dict['title'] = item.title
            # 品类
            info_dict['category'] = category
            # 性别
            info_dict['sex'] = sex
            # 图集 需要请求详情页
            info_dict['atlas'] = ''
            # 成本价 原价对应公价
            info_dict['cost_price'] = item.cost_price.split('$')[1].replace(',', '')
            # 公价
            info_dict['retail_price'] = item.wages.split('$')[1].replace(',', '')
            # 货币单位
            # info_dict['currency'] = task['currency']
            info_dict['currency'] = "USD"

            info_dict['url'] = 'https://www.net-a-porter.com' + item.url if item.url else ''
            # 规格 ，暂时没有
            # info_dict['specifications'] = ''
            # 商品描述。详情页的
            info_dict['product_des'] = ''
            info_dict['size_stock'] = ''
            # 尺码表
            info_list.append(info_dict)

        return info_list


class PorterImageMainExtractor:
    @classmethod
    def get_item(cls, response):
        return cls._extract(response)

    @classmethod
    def _extract(cls, response):
        image_list = list()
        for item in PorterImageMainInfoExtractor.get_items(response):
            image_url = 'http:' + item.image_url.split(' ')[0]
            image_list.append(image_url)
        size_list = PorterSizeMainInfoExtractor.get_item(response).size
        size_other = PorterSizeMainInfoExtractor.get_item(response).size_other
        return image_list, size_list, size_other
